<template>
	<view class="dropdown-container">
		<!-- 遮罩层，点击关闭下拉框 , 有问题，设置dropdown 的层级小于遮罩层的层级没有反应-->
		<!-- 问题解决： 想要fixed的z-index生效， 其父元素的定位不能是是相对定位-->
		<view style="position: absolute;">
			<view :class="{'cover-view': true,'active': isActive}" @click="clickCall"></view>
		</view>
		
		<slot :click="clickCall" class="slot-view"></slot>
		<view :class="{'dropdown': true, 'active': isActive}" :style="{bottom: `${-40*optionItems.length}px`}">
			<view class="dropdown-item" v-for="(item,index) in optionItems" :key="index" @click="handleCommand(item.command)">
				<uni-icons class="icon" color="white" :type="item.icon" size="20"></uni-icons>
				<text class="title">{{item.title}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'DropDown',
		props: {
			/* [
				{
					icon: 'plusempty',
					title: '测试',
					command: 1
				}
			] */
			optionItems: {
				type: Array,
				default: () => {
					return []
				}
			}
		},
		data() {
			return {
				isActive: false,
			}
		},
		methods: {
			clickCall() {
				this.isActive = !this.isActive
			},
			handleCommand(command) {
				this.isActive = !this.isActive
				this.$emit('handleCommand',command)
			}
		}
	}
</script>

<style lang="scss" scoped>
	* {
		box-sizing: border-box;
	}
	view {
		display: inline-block;
	}
	.dropdown-container {
		position: relative;
	}
	.cover-view {
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		z-index: 9997;
		display: none;
	}
	
	.dropdown {
		display: none;
		width: 140px;
		position: absolute;
		z-index: 9999;
		right: 0px;
		border-radius: 10px;
		background-color: $status-bar-backgroud-color;
		.dropdown-item {
			width: 100%;
			height: 40px;
			line-height: 40px;
			display: flex;
			& > .icon {
				display: inline-block;
				height: 100%;
				width: 30%;
			}
			& > .title {
				display: inline-block;
				height: 100%;
				width: 70%;
			}
			&:not(:last-child) > .title {
				border-bottom: 1px solid gray;
			}
		}
	}
	
	.active {
		display: inline-block;
	}
</style>